﻿//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
#include "Effekseer.Random.h"

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
namespace Effekseer
{

int32_t RandLCG(int32_t& seed)
{
	const int64_t a = 1103515245;
	const int64_t c = 12345;
	const int64_t m = 2147483647;

	seed = (seed * a + c) & m;
	return seed % 0x7fff;
}
const int32_t RandLCGMax = 0x7fff;

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void RandObject::SetSeed(int32_t seed)
{
	m_seed = seed;
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
int32_t RandObject::GetRandInt()
{
	return RandLCG(m_seed);
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
float RandObject::GetRand()
{
	auto ret = RandLCG(m_seed);
	return (float)ret / (float)(RandLCGMax - 1);
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
float RandObject::GetRand(float min_, float max_)
{
	return GetRand() * (max_ - min_) + min_;
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
float RandCallback::Rand(void* userData)
{
	IRandObject* rand = (IRandObject*)userData;
	return rand->GetRand();
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
float RandCallback::RandSeed(void* userData, float randSeed)
{
	auto seed = static_cast<int32_t>(randSeed * 1024 * 8);
	return static_cast<float>(RandLCG(seed));
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
} // namespace Effekseer
  //----------------------------------------------------------------------------------
  //
  //----------------------------------------------------------------------------------